package com.haoshuang.zhouzhoubang.ui.page.order.appeal

import android.content.Context
import android.os.Bundle
import androidx.recyclerview.widget.GridLayoutManager
import com.blankj.utilcode.util.ImageUtils
import com.haoshuang.zhouzhoubang.databinding.ActivityAppealBinding
import com.haoshuang.zhouzhoubang.ui.adapter.ChooseImageAdapter
import com.haoshuang.zhouzhoubang.utils.MultiMediaChooseUtils
import com.haoshuang.zhouzhoubang.utils.OSSUploadUtils
import com.haoshuang.zhouzhoubang.widget.UploadFileBean
import com.luck.picture.lib.entity.LocalMedia
import com.mufeng.libs.base.BaseVMActivity
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.ktx.getString
import com.mufeng.libs.utils.view.click
import com.mufeng.libs.utils.startPage
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.load
import java.util.ArrayList

/**
 *  订单申诉
 */
class AppealActivity : BaseVMActivity<AppealViewModel, ActivityAppealBinding>() {

    companion object {

        fun launch(
            context: Context,
            order_id: Int?,
            feedback_id: Int?,
            type: Int?,
            detail: String?,
            images: String?,
        ) {
            context.startPage<AppealActivity>(
                "order_id" to order_id,
                "feedback_id" to feedback_id,
                "type" to type,
                "detail" to detail,
                "images" to images
            )
        }
    }

    private val order_id: Int by lazy { intent.getIntExtra("order_id", 0) }
    private val feedback_id: Int by lazy { intent.getIntExtra("feedback_id", 0) }

    private val type by lazy { intent.getIntExtra("type", 0) }
    private val detail by lazy { intent.getString("detail") }
    private val images by lazy { intent.getString("images") }


    override fun initView(savedInstanceState: Bundle?) {
        binding.titleBar.title = "申诉"
        binding.titleBar.leftImageView().click { finish() }

        binding.btnSubmit.setOnClickListener {
            val content = binding.tvContent.text.toString()
            val imageList = binding.customUploadView1.getMediaList()
            if (imageList.isEmpty()) {
                toast("请选择照片")
                return@setOnClickListener
            }
            if (content.isEmpty()) {
                toast("请输入申诉说明")
                return@setOnClickListener
            }
            val pics = imageList.map { it.url }.joinToString(",")

            if (type == 5) {
                vm.userOrderAppeal(
                    order_id, binding.tvContent.text.toString(),
                    pics ?: ""
                )
            } else {
                vm.appealOrderFeedback(
                    feedback_id,
                    content = binding.tvContent.text.toString(),
                    pics ?: ""
                )
            }
        }
    }


    override fun initData() {
        vm.getServiceOrderDetail(order_id.toString())
    }

    override fun startObserve() {
        vm.getServiceOrderDetailData.observeState(this) {
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess {
                dismissLoading()
                if (it?.data != null) {
                    binding.ivHead.load(it.data?.product_image)
                    binding.tvName.text = it.data?.store_name
                    binding.tvOrder.text = "订单编号:" + it.data?.order_id

                    binding.tvDetail.text = detail
                    binding.customUploadView.setMediaList(
                        images.split(",").map { UploadFileBean(null, it, 0, false, 2) })

                }

            }
        }

        vm.appealOrderFeedbackData.observeState(this) {
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess {
                dismissLoading()
                toast("申诉已提交，请等待平台处理")
                finish()
            }
        }

        vm.userOrderAppealData.observeState(this){
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess {
                dismissLoading()
                toast("申诉已提交，请等待平台处理")
                finish()
            }
        }
    }

}